<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Type;
use App\Shop;
use Illuminate\Support\Facades\DB;
use Redis;
use App\Img;
use App\UserInfo;
use App\Order;
class DemoController extends Controller
{
    public function type(){
        $res=Type::get();
        return $res;
    }
    public function product(){
        $res=Shop::get();
        return json_encode(['message'=>'查询成功','res'=>$res]);
    }
    public function shop(Request $request){
        $id=$request->id;
        $redis=new Redis();
        $redis->connect('127.0.0.1',6379);
        $redis->select(6);
        if($redis->sMembers('date:'.$id)){
            $res=$redis->sMembers('date'.$id);
            return json_encode(['message'=>'redis的数据','res'=>$res]);
        }else{
            $res=Shop::where(['t_id'=>$id])->get();
            $redis->sAdd('date:'.$id,$res);
            return json_encode(['message'=>'数据库的数据','res'=>$res]);
        }

    }
    public function img(){
      $res=Img::get();
      return $res;
    }
    public function search(Request $request){
        $name=$request->name;
        $res = DB::table('shop')
            ->where('shopname', 'like', "%$name%")->orderBy('price','desc')
            ->get();
        return json_encode(['message'=>'查询的数据','res'=>$res]);

    }
    public function shopTail(Request $request)
    {
        $data = Shop::where(['id'=>$request->id])->first();
        return $data;
    }
    public function goBuy(Request $request)
    {
//        var_dump($request->id);die;
        $data = UserInfo::where(['id'=>$request->id])->first();
        $a = json_decode($data,true);
        return $a;
    }
    public function addorder(Request $request){
        $home=$request->home;
        $price=$request->price;
        $u_id=$request->u_id;
        $number=date('Ymd His',time()).rand(999,9999);
        $res=Order::create([
            'uid'=>$u_id,
            'site'=>$home,
            'sum'=>$price,
            'number'=>$number,
            'status'=>0

        ]);
        if($res){
            return json_encode(['code'=>1,'message'=>'添加成功']);
        }else{
            return json_encode(['code'=>0,'message'=>'添加失败']);
        }
    }
    public function buycar(Request $request){
        $goods_id = $request->goods_id;
        $num = $request->num;
        $redis  = new Redis();
        $redis->pconnect('127.0.0.1',6379);
        $uid = $redis->get('uid');
        $name = 'buycar:uid:'.$uid;
        $res = $redis->hset($name,$goods_id,$num);
        return '200';
    }
    public function buy(){
        $redis  = new Redis();
        $redis->pconnect('127.0.0.1',6379);
        $uid = $redis->get('uid');
        $name = 'buycar:uid:'.$uid;
        $data = $redis->hGetAll($name);
        $res = [];
        $str = [];
        foreach ($data as $key => $v){
            $arr['goods_id'] = $key;
            $arr['many'] = $v;
            array_push($res,$arr);
        }

        foreach ($res as $v){
            $btr['goods_id'][] = $v['goods_id'];
            $btr['num'][] = $v['many'];
        }
//        return $btr;

        foreach ($btr['goods_id'] as $k => $v) {
            $deserve[] = Shop::where(['id' => $v])->get();
        }
        foreach ($deserve as $k => $v){
            foreach ($v as $kk => $vv){
                $str[] = $vv;
            }
        }
        foreach ($str as $k => $v){
            foreach ($btr['num'] as $kk => $vv){
                if($k == $kk){
                    $v['num'] = $vv;
                }
            }
        }
        return $str;
    }
}
